package com.qf.controller;

import com.qf.Resp.BaseResp;
import com.qf.operators.Operators;
import com.qf.service.Operators2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
/*@CrossOrigin*/
public class Operators2Controller {

    @Autowired
    Operators2Service operators2Service;

   @RequestMapping("/findAll")
    public BaseResp findAll() {
        List<Operators> list = operators2Service.findAll();
        System.out.println(list);
       BaseResp baseResp = new BaseResp();
       baseResp.setCode(10001l);
       baseResp.setMessage(list);
       return baseResp;
    }

    @RequestMapping("/findById")
    public BaseResp findById(@RequestBody Map map) {
        Integer id = (Integer) map.get("id");
        Operators operators = operators2Service.findById((id));
        System.out.println(operators);
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(10002l);
        baseResp.setMessage(operators);
        return baseResp;
    }

    @RequestMapping("/insertAndUpdate")
    public BaseResp insertAndUpdate(@RequestBody Operators operators) {
        BaseResp baseResp = new BaseResp();
        try {
            operators2Service.updateStudent(operators);
            baseResp.setCode(1200l);
            baseResp.setMessage("success");
            return baseResp;
        } catch (Exception ex) {
            baseResp.setCode(1201l);
            baseResp.setMessage("fail");
            return baseResp;
        }
    }

    @RequestMapping("/deleteById")
    public BaseResp deleteById(@RequestBody Map map) {
        Integer id = (Integer) map.get("id");
        operators2Service.deleteById(id);
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(10003l);
        baseResp.setMessage("删除成功");
        return baseResp;
    }

}
